package com.garbageteam.garbageserver.controller;

import com.garbageteam.garbageserver.model.GarbageOut;
import com.garbageteam.garbageserver.response.BaseResponse;
import com.garbageteam.garbageserver.service.IGarbageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping(value = "/garbage")
public class GarbageController {

    @Autowired
    private IGarbageService garbageService;

    @GetMapping("/classify")
    public ResponseEntity<BaseResponse<GarbageOut>> getGarbage(String garbageName) {
        try {
            if (StringUtils.isEmpty(garbageName)) {
                return new ResponseEntity<>(new BaseResponse<>(-1,"请输入要查询的垃圾名称"), HttpStatus.OK);
            }
            GarbageOut garbage = garbageService.getGarbage(garbageName);
            return new ResponseEntity<>(new BaseResponse<>(200,"success",garbage),HttpStatus.OK);
        } catch (Exception e) {
            log.error("查询垃圾分类信息发生异常", e);
            return new ResponseEntity<>(new BaseResponse<>(500,"服务器内部错误"), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @GetMapping(value = "/list")
    public ResponseEntity<BaseResponse<Page<GarbageOut>>> getGarbageList(Integer garbageType, Pageable pageable) {
        try {
            Page<GarbageOut> garbageList = garbageService.getGarbageList(garbageType, pageable);
            return new ResponseEntity<>(new BaseResponse<>(200,"success",garbageList),HttpStatus.OK);
        } catch (Exception e) {
            log.error("GarbageController.getGarbageList error", e);
            return new ResponseEntity<>(new BaseResponse<>(500,"服务器内部错误"), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

}
